'''
【02xpath的使用】中【03top250电影抓取代码优化.py】
 设置 url=https://movie.douban.com/top250?start=100&filter=
 还可以使用params = {
                    'start': '100',
                    'filter': ''
                  }
 完成
'''
import requests
from lxml import etree

if __name__== '__main__':
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
    }
    url = 'https://movie.douban.com/top250'
    params = {
        'start': '100',
        'filter': ''
    }
    response = requests.get(url=url, headers=headers, params=params)
    page = response.text
    html = etree.HTML(page)
    data_list = html.xpath("//ol/li")
    num = 1
    for li in data_list:
        title = li.xpath('.//span[@class="title"][1]/text()')
        source = li.xpath('.//span[@class="rating_num"][1]/text()')
        sub = li.xpath('.//span[@class="inq"][1]/text()')
        evaluate = li.xpath('.//div[@class="star"]/span[last()]/text()')
        title0 = title[0] if len(title) > 0 else ''
        source0 = source[0] if len(source) > 0 else ''
        sub0 = sub[0] if len(sub) > 0 else ''
        evaluate0 = evaluate[0] if len(evaluate) > 0 else ''
        print(f'{num}.电影名称:{title0},评分:{source0},类型:{sub0},评价人数:{evaluate0}')
        num = num + 1
